package com.ts.content.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ts.common.core.utils.Result;
import com.ts.common.core.validator.AssertUtils;
import com.ts.common.core.validator.ValidatorUtils;
import com.ts.common.core.validator.group.AddGroup;
import com.ts.common.core.validator.group.DefaultGroup;
import com.ts.common.core.validator.group.UpdateGroup;
import com.ts.common.mybatis.page.PageData;
import com.ts.content.dto.UserDTO;
import com.ts.content.entity.UserEntity;
import com.ts.content.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.sql.Wrapper;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author toString toString@qq.com
 * @since 1.0.0 2023-02-27
 */
@RestController
@RequestMapping("/user")
@Api(tags="")
public class UserController {
    @Autowired
    private UserService userService;


    @GetMapping("/getUser/{id}")
    public UserEntity getUser(@PathVariable Long id){
        return userService.getOne(new QueryWrapper<UserEntity>().eq("id",id));
    }


    @GetMapping("/getUserByMobile/{mobile}")
    public UserEntity getUserByMobile(@PathVariable String mobile) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        return userService.getOne(queryWrapper);
    }

    @PostMapping("/addUser")
    public UserEntity addUser(@RequestBody UserEntity userEntity) {
        userService.save(userEntity); // MP会自动回填主键
        return userEntity; // 返回的目的是为了获取主键
    }


    @GetMapping("page")
    @ApiOperation("分页")
    public Result<Page<UserEntity>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        //PageData<UserEntity> page = userService.page(params);
        Page<UserEntity> objectPage = new Page<>();
        objectPage.setCurrent((Long) params.get("current"));
        Page<UserEntity> page = userService.page(objectPage);
        return new Result<Page<UserEntity>>().ok(page);
    }


    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<UserEntity> get(@PathVariable("id") Long id){
        //UserDTO data = userService.get(id);
        UserEntity userEntity = userService.getById(id);
        return new Result<UserEntity>().ok(userEntity);
    }

    @DeleteMapping
    @ApiOperation("删除")

    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        userService.removeBatchByIds(Arrays.asList(ids));
        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        //List<UserDTO> list = userService.list(params);

        //ExcelUtils.exportExcelToTarget(response, null, list, UserExcel.class);
    }

    /**
     * 修改个人信息
     * @param userEntity
     * @return
     */
    @PutMapping
    public Result update(@RequestBody UserEntity userEntity){
        userService.updateById(userEntity);
        return new Result();
    }

}